package cn.iocoder.yudao.module.alipay.controller.admin.infoorganize;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.alipay.controller.admin.infoorganize.vo.*;
import cn.iocoder.yudao.module.alipay.dal.dataobject.infoorganize.InfoOrganizeDO;
import cn.iocoder.yudao.module.alipay.convert.infoorganize.InfoOrganizeConvert;
import cn.iocoder.yudao.module.alipay.service.infoorganize.InfoOrganizeService;

@Tag(name = "管理后台 - 机构详情")
@RestController
@RequestMapping("/alipay/info-organize")
@Validated
public class InfoOrganizeController {

    @Resource
    private InfoOrganizeService infoOrganizeService;

    @PostMapping("/create")
    @Operation(summary = "创建机构详情")
    @PreAuthorize("@ss.hasPermission('alipay:info-organize:create')")
    public CommonResult<Long> createInfoOrganize(@Valid @RequestBody InfoOrganizeCreateReqVO createReqVO) {
        return success(infoOrganizeService.createInfoOrganize(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新机构详情")
    @PreAuthorize("@ss.hasPermission('alipay:info-organize:update')")
    public CommonResult<Boolean> updateInfoOrganize(@Valid @RequestBody InfoOrganizeUpdateReqVO updateReqVO) {
        infoOrganizeService.updateInfoOrganize(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除机构详情")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('alipay:info-organize:delete')")
    public CommonResult<Boolean> deleteInfoOrganize(@RequestParam("id") Long id) {
        infoOrganizeService.deleteInfoOrganize(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得机构详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('alipay:info-organize:query')")
    public CommonResult<InfoOrganizeRespVO> getInfoOrganize(@RequestParam("id") Long id) {
        InfoOrganizeDO infoOrganize = infoOrganizeService.getInfoOrganize(id);
        return success(InfoOrganizeConvert.INSTANCE.convert(infoOrganize));
    }

    @GetMapping("/list")
    @Operation(summary = "获得机构详情列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('alipay:info-organize:query')")
    public CommonResult<List<InfoOrganizeRespVO>> getInfoOrganizeList(@RequestParam("ids") Collection<Long> ids) {
        List<InfoOrganizeDO> list = infoOrganizeService.getInfoOrganizeList(ids);
        return success(InfoOrganizeConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得机构详情分页")
    @PreAuthorize("@ss.hasPermission('alipay:info-organize:query')")
    public CommonResult<PageResult<InfoOrganizeRespVO>> getInfoOrganizePage(@Valid InfoOrganizePageReqVO pageVO) {
        PageResult<InfoOrganizeDO> pageResult = infoOrganizeService.getInfoOrganizePage(pageVO);
        return success(InfoOrganizeConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出机构详情 Excel")
    @PreAuthorize("@ss.hasPermission('alipay:info-organize:export')")
    @OperateLog(type = EXPORT)
    public void exportInfoOrganizeExcel(@Valid InfoOrganizeExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<InfoOrganizeDO> list = infoOrganizeService.getInfoOrganizeList(exportReqVO);
        // 导出 Excel
        List<InfoOrganizeExcelVO> datas = InfoOrganizeConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "机构详情.xls", "数据", InfoOrganizeExcelVO.class, datas);
    }

}
